<?php

namespace App\Models;

use App\Util\Tool;
use Illuminate\Database\Eloquent\Model;

class Culture extends Model
{
    protected $casts = [
        'detail_images' => 'json',
    ];

    protected $table = "c_cultures";

    /**
     * @param $pictures
     * @return void
     * 多图片添加使用
     */
    public function setDetailImagesAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['detail_images'] = json_encode($pictures);
        }
    }

    /**
     * @param $pictures
     * @return mixed
     * 多图片读取使用
     */
    public function getDetailImagesAttribute($pictures)
    {
        return json_decode($pictures, true);
    }


    /**
     * @param $param
     * @return array
     * 平台文化
     */
    public function getCulturesDetail($param)
    {
        $res = $this->where([['id', '=', $param['id']]])->first();
        if (!empty($res)) {
            $data = $res->toArray();
            if (!empty($data['detail_images']) && is_array($data['detail_images'])) {
                foreach ($data['detail_images'] as $k => $v) {
                    $data['detail_images'][$k] = env("APP_URL") . '/upload/' . $v;
                }
            }else{
                $data['detail_images'] =  env("APP_URL") . '/upload/' .  $data['detail_images'];
            }
            return $data;
        }
        return [];
    }
}
